package com.enjia.common.framework;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;


import com.enjia.common.utils.ViewFinder;
import com.enjia.common.view.CustomProgressDialog;
import com.enjia.enjiashop.R;

import butterknife.ButterKnife;

/***
 * 公共基类
 */
public abstract class BaseActivity extends AppCompatActivity{
	protected Context context;

	//缓冲Dialog的调用
	private CustomProgressDialog cusProgressDialog;
	// 当前画面是否设定沉浸式状态栏  默认设定是
	protected boolean isStatusBar = false;
	// 当前画面特殊设定沉浸式状态栏的颜色
	protected String statusBarColor = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		context = this;
		setContentViewBefore();

		setContentView(R.layout.activity_base);
		ViewFinder viewFinder=new ViewFinder(this);
		if (getLayoutResId() != 0) {
			//View view = View.inflate(this, getLayoutResId(), null);
			View view = this.getLayoutInflater().inflate(getLayoutResId(),null);
			ViewGroup vg = viewFinder.find(R.id.baseActityContent);
			vg.addView(view);
		}

		//初始化ButterKnife；
		ButterKnife.bind(this);
		// 初始化画面view
		initView(savedInstanceState);

	}

	@Override
	public Resources getResources() {
		Resources res = super.getResources();
		Configuration config = new Configuration();
		config.setToDefaults();
		res.updateConfiguration(config,res.getDisplayMetrics() );
		return res;
	}

	protected void setContentViewBefore(){

	}

//	protected void showEmpty(String str) {
//		showEmpty(true, str, true);
//	}
//
//	protected void showEmpty(String str, boolean goHomeFlag) {
//		showEmpty(true, str, goHomeFlag);
//	}
//
//	protected void showEmpty(boolean imgShowFlag, String str, boolean goHomeFlag) {
//
//	}
//
//	protected void showEmpty(boolean imgShowFlag, String str, boolean goHomeFlag, String strButonText, OnClickListener ol) {
//
//	}
//
//	protected void showError(String str) {
//		showError(true, str, true);
//	}
//
//	protected void showError(String str, boolean goHomeFlag) {
//		showError(true, str, goHomeFlag);
//	}
//
//	protected void showError(boolean imgShowFlag, String str, boolean goHomeFlag) {
//
//	}

	/**
	 * 是否沉浸式状态栏
	 * @return
     */
	protected  boolean isStatusBar() {
		return isStatusBar;
	}

	/**
	 * 设定是否沉浸式状态栏
	 * @return
	 */
	protected  void setStatusBar(boolean isStatusBarJoin) {
		this.isStatusBar = isStatusBarJoin;
	}

	/**
	 * 设定是否沉浸式状态栏 以及此时状态栏的颜色
	 * @return
	 */
    public void setStatusBar(boolean isStatusBarJoin, String statusBarColorString) {
		this.isStatusBar = isStatusBarJoin;
		this.statusBarColor = statusBarColorString;
	}

	/**
	 * 里头是否包含fragment(涉及到统计)
	 * @return
     */
	protected boolean isFragmentContainer(){return false;};

	private AlertDialog mAlertDialog;
	@Override
	protected void onStop() {
		super.onStop();
		if (mAlertDialog != null && mAlertDialog.isShowing()) {
			mAlertDialog.dismiss();
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	protected abstract void initView(Bundle savedInstanceState);

	protected abstract int getLayoutResId();

	/**
	 * Requests given permission.
	 * If the permission has been denied previously, a Dialog will prompt the user to grant the
	 * permission, otherwise it is requested directly.
	 */
	protected void requestPermission(final String permission, String rationale, final int requestCode) {
		if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
			showAlertDialog("Permission needed", rationale,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							ActivityCompat.requestPermissions((Activity) context,
									new String[]{permission}, requestCode);
						}
					}, "ok", null, "cancel");
		} else {
			ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
		}
	}


	/**
	 * 显示对话框
	 * @param title
	 * @param message
	 * @param onPositiveButtonClickListener
	 * @param positiveText
	 * @param onNegativeButtonClickListener
     * @param negativeText
     */
	protected void showAlertDialog(@Nullable String title, @Nullable String message,
                                   @Nullable DialogInterface.OnClickListener onPositiveButtonClickListener,
                                   @NonNull String positiveText,
                                   @Nullable DialogInterface.OnClickListener onNegativeButtonClickListener,
                                   @NonNull String negativeText) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.PaiLeDialog);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(positiveText, onPositiveButtonClickListener);
		builder.setNegativeButton(negativeText, onNegativeButtonClickListener);
		mAlertDialog = builder.show();
	}

	public Handler getHandler() {
		return new Handler();
	}

	public Activity getContext() {
		return (Activity) context;
	}

	/**
	 * 显示缓冲Dialog
	 */
	public void showProgressDialog(Context mContext,String message){
		if(null==cusProgressDialog){
			cusProgressDialog=new CustomProgressDialog(mContext,message);
		}
		if(cusProgressDialog!=null){
			cusProgressDialog.show();
		}
	}
	/**
	 * 销毁缓冲Dialog
	 */
	public void destoryProgressDialog(){
		if(null != cusProgressDialog && cusProgressDialog.isShowing()){
			cusProgressDialog.dismiss();
			cusProgressDialog = null;
		}
	}
}
